package main

import (
	"context"
	"fmt"
	"go.etcd.io/etcd/clientv3"
	"time"
)
// etcd watch


func main() {
	cli, err := clientv3.New(clientv3.Config{Endpoints:
	[]string{"127.0.0.1:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		// handle error!
		fmt.Printf("connect to etcd failed, err:%v\n", err)
		return
	}
	fmt.Println("connect to etcd success")
	defer cli.Close()

	// watch
	// 排一个哨兵，一直监视key的变化(新增、修改、删除)
	ch := cli.Watch(context.Background(), "skyworth")
	// 从通道尝试取值（监视的信息）
	for wresp := range ch{
		for _,evt := range wresp.Events{
			fmt.Printf("Type: %v Key:%v value:%v\n",evt.Type,string(evt.Kv.Key),string(evt.Kv.Value))
		}
	}

}